﻿using Enough.Lifecycle.Common;
using Enough.Lifecycle.Helper;
using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Enough.Lifecycle.View
{
    public class BasePage : Page
    {
        public delegate void CreationHandler(BasePage page);

        /// <summary>
        /// Register a CreationHandler delegate for getting notified whenever a BasePage is created.
        /// </summary>
        public static CreationHandler Created;

        private static BasePage _current;
        public static BasePage Current
        {
            private set
            {
                if (_current == null)
                {
                    try
                    {
                        Application.Current.Resuming += onBasePageResuming;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("unable to set Application.Current.Resuming handler: " + ex);
                    }
                }
                _current = value;
            }

            get
            {
                return _current;
            }
        }

        private static void onBasePageResuming(object sender, object e)
        {
            BasePage current = _current;
            if (current != null)
            {
                current.RegisterHandlers();
            }
        }

        private NavigationHelper _navigationHelper;

        public BasePage()
        {
            _navigationHelper = new NavigationHelper(this);
            _navigationHelper.LoadState += LoadState;
            _navigationHelper.SaveState += SaveState;
            //Loaded += BasePage_Loaded;
            //Unloaded += BasePage_Unloaded;
            Current = this;
            if (Created != null)
            {
                Created(this);
            }
        }

        /// <summary>
        /// Allows subclasses to save their state.
        /// The default implementation is emtpy.
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the state arguments</param>
        protected virtual void SaveState(object sender, SaveStateEventArgs e)
        {
            // let subclasses implement this if needed
        }

        /// <summary>
        /// Allows subclasses to load their state.
        /// The default implementation is emtpy.
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the state arguments</param>
        protected virtual void LoadState(object sender, LoadStateEventArgs e)
        {
            // let subclasses implement this if needed
        }
        
        /// <summary>
        /// Register handlers like Messenger.Register<>() here.
        /// </summary>
        protected virtual void RegisterHandlers()
        {
            // allow subclasses to register handlers
        }

        /// <summary>
        /// Deregister handlers like Messenger.Register<>() here.
        /// </summary>
        protected virtual void DeregisterHandlers()
        {
            // allow subclasses to deregister handlers
        }

        void BasePage_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
        }

        void BasePage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
        }


        #region NavigationHelper registration

        /// <summary>
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// <para>
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="NavigationHelper.LoadState"/>
        /// and <see cref="NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.
        /// </para>
        /// </summary>
        /// <param name="e">Provides data for navigation methods and event
        /// handlers that cannot cancel the navigation request.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RegisterHandlers();
            this._navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            DeregisterHandlers();
            this._navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        public string Localize(string resourceName, params object[] parameters)
        {
            return LocalizationHelper.Localize(resourceName, parameters);
        }

    }
}
